package com.sskj.finance.presenter;

import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ToastUtil;
import com.sskj.finance.bean.HomeBorrowGetTransrecordBean;
import com.sskj.finance.bean.RateBean;
import com.sskj.finance.bean.SupplyData;
import com.sskj.finance.http.HttpConfig;
import com.sskj.finance.ui.fragment.DYJKFragment;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.http.JsonConverter;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;


public class DYJKFragmentPresenter extends BasePresenter<DYJKFragment> {
    /**
     * 抵押记录
     *
     * @param type   1押币借钱 2压钱借币
     * @param status 1计息中 2已赎回
     * @param p      页码，默认1
     * @param size   页条数，默认10
     * @return
     */
    public Flowable<List<HomeBorrowGetTransrecordBean>> homeBorrowGetTransrecord(String type, String status,String pid, String p, String size) {
        return httpService.homeBorrowGetTransrecord(type, status, pid,p, size)
                .converter(new JsonConverter<HttpData<PageBean<HomeBorrowGetTransrecordBean>>>() {
                })
                .adapt(new FlowableBody<>())
                .map(pageBeanHttpData -> pageBeanHttpData.getData().getList())
                .onErrorReturnItem(new ArrayList<>());
    }

    /**
     * 还款
     *
     * @param order_id 借贷记录id
     * @return
     */
    public void homeBorrowPayback(String order_id,String tpwd) {
        httpService.homeBorrowPayback(order_id,tpwd).execute(new JsonCallBack<HttpData>(this) {
            @Override
            public void onSuccess(Response<HttpData> response) {
                HttpData httpData = response.body();
                if (httpData.getStatus() == HttpConfig.OK) {
                    mView.backSuccess();

                }
            }
        });
    }

    public void getSupplyData(String code) {
        httpService.getSupplyCode(code)
                .execute(new JsonCallBack<HttpData<SupplyData>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<SupplyData>> response) {
                        HttpData<SupplyData> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.showSupply(httpData.getData());

                        }
                    }
                });
    }

    /**
     * 补仓
     *
     * @param id
     * @param code
     * @param num
     * @param pwd
     */
    public void supply(String id, String code, String num, String pwd) {
        httpService.supply(id, code, num, pwd)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData httpData = response.body();
                        ToastUtil.showShort(httpData.getMsg());
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.supplySuccess();
                        }
                    }
                });
    }

    public void getRate(String tran_id) {
        httpService.getBorrowRate(tran_id)
                .execute(new JsonCallBack<HttpData<RateBean>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<RateBean>> response) {
                        HttpData<RateBean> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.showCancel(httpData.getData());
                        }
                    }
                });
    }
}
